#!/usr/bin/env python3
#
# Ubuntu aarch64 image
#
# Copyright 2020 Linaro
#
# Authors:
#  Robert Foley <robert.foley@linaro.org>
#  Originally based on ubuntu.i386 Fam Zheng <famz@redhat.com>
#
# This code is licensed under the GPL version 2 or later.  See
# the COPYING file in the top-level directory.
#

import sys
import basevm
import aarch64vm
import ubuntuvm

DEFAULT_CONFIG = {
    'cpu'          : "cortex-a57",
    'machine'      : "virt,gic-version=3",
    'install_cmds' : "apt-get update,"\
                     "apt-get build-dep -y --arch-only qemu,"\
                     "apt-get install -y libfdt-dev pkg-config language-pack-en ninja-build",
    # We increase beyond the default time since during boot
    # it can take some time (many seconds) to log into the VM
    # especially using softmmu.
    'ssh_timeout'  : 60,
}

class UbuntuAarch64VM(ubuntuvm.UbuntuVM):
    name = "ubuntu.aarch64"
    arch = "aarch64"
    image_name = "ubuntu-18.04-server-cloudimg-arm64.img"
    image_link = "https://cloud-images.ubuntu.com/releases/18.04/release/" + image_name
    image_sha256="0fdcba761965735a8a903d8b88df8e47f156f48715c00508e4315c506d7d3cb1"
    BUILD_SCRIPT = """
        set -e;
        cd $(mktemp -d);
        sudo chmod a+r /dev/vdb;
        tar --checkpoint=.10 -xf /dev/vdb;
        ./configure {configure_opts};
        make --output-sync {target} -j{jobs} {verbose};
    """
    def boot(self, img, extra_args=None):
        aarch64vm.create_flash_images(self._tmpdir, self._efi_aarch64)
        default_args = aarch64vm.get_pflash_args(self._tmpdir)
        if extra_args:
            extra_args.extend(default_args)
        else:
            extra_args = default_args
        # We always add these performance tweaks
        # because without them, we boot so slowly that we
        # can time out finding the boot efi device.
        if '-smp' not in extra_args and \
           '-smp' not in self._config['extra_args'] and \
           '-smp' not in self._args:
            # Only add if not already there to give caller option to change it.
            extra_args.extend(["-smp", "8"])

        # We have overridden boot() since aarch64 has additional parameters.
        # Call down to the base class method.
        super(UbuntuAarch64VM, self).boot(img, extra_args=extra_args)

if __name__ == "__main__":
    defaults = aarch64vm.get_config_defaults(UbuntuAarch64VM, DEFAULT_CONFIG)
    sys.exit(basevm.main(UbuntuAarch64VM, defaults))
